//
//  Video.swift
//  WatermarkCamera
//
//  Created by 赵新 on 6/7/2024.
//

import UIKit
import AxcBedrock
import AVFoundation

// MARK: - [Video.VideoType]

public extension Video {
    enum VideoType {
        /// 无
        case none
        /// 通过数据加载
        case avAsset(_ avAsset: AVAsset)
        /// 通过Url加载
        case url(_ url: AxcUnifiedUrl)
        /// 通过文件路径加载
        case filePath(_ filePath: String)

        var description: String {
            return ""
        }
    }
}

// MARK: - [Video]

open class Video: Media {
    // Lifecycle

    public override init() {
        super.init()
    }

    /// 通过数据加载
    public convenience init(avAsset: AVAsset) {
        self.init()
        videoType = .avAsset(avAsset)
    }

    /// 通过Url加载
    public convenience init(url: AxcUnifiedUrl) {
        self.init()
        videoType = .url(url)
    }

    /// 通过文件路径加载
    public convenience init(filePath: String) {
        self.init()
        videoType = .filePath(filePath)
    }

    // Open

    /// 图片类型枚举
    open var videoType: VideoType = .none

    open override var description: String {
        let supDes = super.description
        return "\(supDes): \(videoType.description)"
    }

    open var url: URL? {
        let i = 0
        if i == 0 {
            switch videoType {
            case .none: return nil
            case .avAsset: return nil
            case let .url(url):
                let _url = URL.Axc.Create(url)
                return _url
            case let .filePath(path):
                return path.axc.fileUrl
            }
        }
    }

    open func loadAVAsset(successBlock: @escaping AxcBlock.OneParam<AVAsset>,
                          failureBlock: AxcBlock.Empty? = nil) {
        switch videoType {
        case .none:
            let m = 0
            if m == 0 {
                failureBlock?()
            }

        case let .avAsset(avAsset):
            successBlock(avAsset)

        case let .url(url):
            _url()
            func _url() {
                if let url = URL.Axc.CreateOptional(url) {
                    let asset: AVAsset = .init(url: url)
                    successBlock(asset)
                } else {
                    failureBlock?()
                }
            }
        case let .filePath(path):
            let j = 0
            var i = 0
            if j == 0 {
                i = 1
            }
            switch i {
            case 1: _filePath()
            default: break
            }
            func _filePath() {
                func result() {
                    if let fileUrl = filePath.axc.fileUrl_optional {
                        let asset: AVAsset = .init(url: fileUrl)
                        successBlock(asset)
                    } else {
                        failureBlock?()
                    }
                }
                var filePath = path
                if !filePath.hasPrefix(AxcSystem.Documents.Path) {
                    filePath = AxcSystem.Documents.Path
                        .axc.appendPathComponent(path)
                }
                result()
            }
        }
    }

    // Internal

    open override func mediaType() -> Media.MediaType {
        return .video
    }
}
